<!doctype html>
<html lang="zh-CN">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="referrer" content="no-referrer-when-downgrade">
    

    <title>35分钟教你学会dart | 早起的年轻人</title>
    <meta property="og:title" content="35分钟教你学会dart - 早起的年轻人">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2021-09-09T11:05:12&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2021-09-09T11:05:12&#43;08:00'>
        
    <meta name="Keywords" content="Flutter，golang,go语言,go语言笔记,飞雪无情,java,android,博客,项目管理,python,软件架构,公众号,小程序">
    <meta name="description" content="35分钟教你学会dart">
        
    <meta name="author" content="luckly">
    <meta property="og:url" content="https://luckly.work/post/dart_basic/35%E5%88%86%E9%92%9F%E6%95%99%E4%BD%A0%E5%AD%A6%E4%BC%9Adart/">
    <link rel="shortcut icon" href='/favicon.ico'  type="image/x-icon">

    <link rel="stylesheet" href='/css/normalize.css'>
    <link rel="stylesheet" href='/css/style.css'>
    <script type="text/javascript" src="//cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    
    
    
        <link href="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" rel="stylesheet">
    
    
    
    
        <link rel="stylesheet" href='/css/douban.css'>
    
        <link rel="stylesheet" href='/css/other.css'>
    
</head>

<link rel="stylesheet"
      href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://luckly.work/">
                        早起的年轻人
                    </a>
                
                <p class="description">专注于Flutter、Android、Java、Go语言(golang)、移动互联网、项目管理、软件架构</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="https://luckly.work/">首页</a>
                    
                    <a  href="https://luckly.work/categories/" title="分类">分类</a>
                    
                    <a  href="https://luckly.work/tags/" title="标签">标签</a>
                    
                    <a  href="https://luckly.work/archives/" title="归档">归档</a>
                    
                    <a  href="https://luckly.work/about/" title="关于我">关于我</a>
                    
                    <a  href="https://github.com/ITmxs/" title="github">github</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    <style type="text/css">
    .post-toc {
        position: fixed;
        width: 200px;
        margin-left: -210px;
        padding: 5px 10px;
        font-family: Athelas, STHeiti, Microsoft Yahei, serif;
        font-size: 12px;
        border: 1px solid rgba(0, 0, 0, .07);
        border-radius: 5px;
        background-color: rgba(255, 255, 255, 0.98);
        background-clip: padding-box;
        -webkit-box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        word-wrap: break-word;
        white-space: nowrap;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        z-index: 999;
        cursor: pointer;
        max-height: 70%;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .post-toc .post-toc-title {
        width: 100%;
        margin: 0 auto;
        font-size: 20px;
        font-weight: 400;
        text-transform: uppercase;
        text-align: center;
    }

    .post-toc .post-toc-content {
        font-size: 15px;
    }

    .post-toc .post-toc-content>nav>ul {
        margin: 10px 0;
    }

    .post-toc .post-toc-content ul {
        padding-left: 20px;
        list-style: square;
        margin: 0.5em;
        line-height: 1.8em;
    }

    .post-toc .post-toc-content ul ul {
        padding-left: 15px;
        display: none;
    }

    @media print,
    screen and (max-width:1057px) {
        .post-toc {
            display: none;
        }
    }
</style>
<div class="post-toc" style="position: absolute; top: 188px;">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#入门">入门</a></li>
    <li><a href="#为什么是flutter">为什么是flutter？</a></li>
    <li><a href="#核心概念">核心概念</a></li>
    <li><a href="#变量注释和数据类型">变量、注释和数据类型</a>
      <ul>
        <li><a href="#注释">注释</a></li>
        <li><a href="#数据类型">数据类型</a></li>
        <li><a href="#基本dart类型">基本dart类型</a></li>
        <li><a href="#动态关键字">动态关键字</a></li>
        <li><a href="#布尔值">布尔值</a></li>
      </ul>
    </li>
    <li><a href="#操作符">操作符</a>
      <ul>
        <li><a href="#算术运算符">算术运算符</a></li>
        <li><a href="#等号运算符">等号运算符</a></li>
        <li><a href="#比较运算符">比较运算符</a></li>
        <li><a href="#逻辑运算符">逻辑运算符</a></li>
      </ul>
    </li>
    <li><a href="#字符串">字符串</a>
      <ul>
        <li><a href="#转义字符串">转义字符串</a></li>
      </ul>
    </li>
    <li><a href="#不变性">不变性</a></li>
    <li><a href="#可空性">可空性</a>
      <ul>
        <li><a href="#空感知运算符">空感知运算符</a></li>
      </ul>
    </li>
    <li><a href="#控制流">控制流</a></li>
    <li><a href="#条件句">条件句</a>
      <ul>
        <li><a href="#如果语句">如果语句</a></li>
        <li><a href="#其他语句">其他语句</a></li>
      </ul>
    </li>
    <li><a href="#while-循环">While 循环</a>
      <ul>
        <li><a href="#测试-while-循环">测试 While 循环</a></li>
        <li><a href="#尝试-do-while-循环">尝试 Do-While 循环</a></li>
      </ul>
    </li>
    <li><a href="#继续和中断">继续和中断</a></li>
    <li><a href="#for-循环">For 循环</a></li>
  </ul>

  <ul>
    <li><a href="#列表">列表</a>
      <ul>
        <li><a href="#使用列表元素">使用列表元素</a></li>
      </ul>
    </li>
    <li><a href="#map">map</a></li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#定义函数">定义函数</a></li>
        <li><a href="#使用函数">使用函数</a></li>
        <li><a href="#嵌套函数">嵌套函数</a></li>
        <li><a href="#可选参数">可选参数</a></li>
        <li><a href="#命名参数和默认值">命名参数和默认值</a></li>
        <li><a href="#匿名函数">匿名函数</a></li>
        <li><a href="#使用匿名函数">使用匿名函数</a></li>
      </ul>
    </li>
    <li><a href="#然后去哪儿">然后去哪儿？</a></li>
  </ul>
</nav>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var postToc = $(".post-toc");
        if (postToc.length) {
            var leftPos = $("#main").offset().left;
            if(leftPos<220){
                postToc.css({"width":leftPos-10,"margin-left":(0-leftPos)})
            }

            var t = postToc.offset().top - 20,
                a = {
                    start: {
                        position: "absolute",
                        top: t
                    },
                    process: {
                        position: "fixed",
                        top: 20
                    },
                };
            $(window).scroll(function () {
                var e = $(window).scrollTop();
                e < t ? postToc.css(a.start) : postToc.css(a.process)
            })
        }
    })
</script>
    <article class="post">
        <header>
            <h1 class="post-title">35分钟教你学会dart</h1>
        </header>
        <date class="post-meta meta-date">
            2021年9月9日
        </date>
        
        <div class="post-meta">
            <span>|</span>
            
            <span class="meta-category"><a href='/categories/Flutter'>Flutter</a></span>
            
            <span class="meta-category"><a href='/categories/dart'>dart</a></span>
            
        </div>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="post-content">
            <p>在本教程中，您将了解 Dart 基础知识，例如：</p>
<ul>
<li>变量、数据类型和运算符</li>
<li>条件和循环</li>
<li>collection</li>
<li>function</li>
</ul>
<p>完成后，您就可以直接使用 Dart 进行 Flutter 开发了。</p>
<h2 id="入门">入门</h2>
<p>单击页面顶部或底部的“*下载资料”*按钮下载示例代码。您不会在本教程中构建示例项目，但可以使用编码示例作为参考。</p>
<p>您可以将<em>main.dart 中</em>的代码粘贴到 DartPad 中，也可以使用<em>Dart SDK</em>运行该文件。</p>
<p>要快速入门，最好使用开源工具<a href="https://dartpad.dev/"><em>DartPad</em></a>，它可以让您通过 Web 浏览器编写和测试 Dart 代码：</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/dartpad-1.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dartpad-1-650x329.png">
            <img class="mx-auto" alt="DartPad 编辑器，注释以显示下方每个元素的位置" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dartpad-1-650x329.png" />
        </a>
    </a></p>
<p>DartPad 的设置类似于典型的 IDE。它包括以下组件：</p>
<ul>
<li><em>编辑器窗格</em>：位于左侧。您的代码将放在此处。</li>
<li><em>RUN 按钮</em>：在编辑器中运行代码。</li>
<li><em>控制台</em>：位于右上角，显示输出。</li>
<li><em>文档面板</em>：位于右下角，显示有关代码的信息。</li>
<li><em>示例</em>：此下拉列表显示了一些示例代码。</li>
<li><em>Null Safety 按钮</em>：使用此<em>按钮</em>可选择加入 Dart 的新声音零安全功能。</li>
<li><em>版本信息</em>：在右下角，DartPad 显示当前使用的 Flutter 和 Dart 版本。</li>
</ul>
<p>如果您愿意，可以在您的机器上本地安装 Dart SDK。一种方法是<a href="https://flutter.dev/docs/get-started/install">安装 Flutter SDK</a>。安装 Flutter 也会安装 Dart SDK。</p>
<p>要直接安装 Dart SDK，请访问https://dart.dev/get-dart。</p>
<h2 id="为什么是flutter">为什么是flutter？</h2>
<p>Dart 与 Java、C#、Swift 和 Kotlin 等其他语言有许多相似之处。它的一些功能包括：</p>
<ul>
<li>静态类型</li>
<li>类型推断</li>
<li>字符串表达式</li>
<li>多范式，包括面向对象和函数式编程</li>
<li>空安全</li>
</ul>
<p>Dart 已针对在各种平台上开发快速应用程序进行了优化。</p>
<h2 id="核心概念">核心概念</h2>
<p>Dart 程序以调用<code>main</code>. Dart 的语法<code>main</code>看起来类似于 C、Swift 或 Kotlin 等其他语言的语法。</p>
<p>清除默认 DartPad 中的所有代码并添加<code>main</code>到编辑器中：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">void</span> main() {
  
}
</code></pre></div><p>你会看到之前有一个返回类型<code>main</code>。在这种情况下，它是<code>void</code>，意味着<code>main</code>不会返回任何东西。</p>
<p>后面的括号<code>main</code>表示这是一个函数定义。大括号包含函数体。</p>
<p>在里面<code>main</code>，你为你的程序添加 Dart 代码。</p>
<p>接下来，您将了解有关以下核心概念的更多信息：</p>
<ul>
<li>变量、注释和数据类型</li>
<li>基本dart类型</li>
<li>Operators</li>
<li>字符串</li>
<li>不变性</li>
<li>可空性</li>
<li>条件和中断</li>
<li>For 循环</li>
</ul>
<p>是时候深入了解了</p>
<h2 id="变量注释和数据类型">变量、注释和数据类型</h2>
<p>您要添加的第一件事<code>main</code>是变量赋值语句。<em>变量</em>保存您的程序将处理的数据。</p>
<p>您可以将变量视为计算机内存中保存值的盒子。每个框都有一个名称，即变量的名称。要使用 Dart 表示变量，请使用<code>var</code>关键字。</p>
<p>添加一个新变量到<code>main</code>：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> myAge <span style="color:#f92672">=</span> <span style="color:#ae81ff">35</span>;  


</code></pre></div><p>每个 Dart 语句都以分号结尾，就像 C 和 Java 中的语句一样。在上面的代码中，您创建了一个变量<code>myAge</code>，并将其设置为等于<em>35</em>。</p>
<p>您可以使用<code>print</code>Dart 中的内置将变量打印到控制台。在变量之后添加该调用：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">print(myAge); <span style="color:#75715e">// 35
</span><span style="color:#75715e"></span>
</code></pre></div><p>在 DartPad 中单击<em>RUN</em>以运行代码。您将在控制台中看到变量的值<em>35</em>。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/first_output-2.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/first_output-2-650x339.png">
            <img class="mx-auto" alt="第一次飞镖输出" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/first_output-2-650x339.png" />
        </a>
    </a></p>
<h3 id="注释">注释</h3>
<p>Dart 中的注释与 C 和其他语言中的注释类似：后面的文本<code>//</code>是单行注释，而其中的文本<code>/* ... */</code>是多行注释块。</p>
<p>下面是一个例子：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#75715e">// This is a single-line comment.
</span><span style="color:#75715e"></span>
print(myAge); <span style="color:#75715e">// This is also a single-line comment.
</span><span style="color:#75715e"></span>
<span style="color:#75715e">/*
</span><span style="color:#75715e"> This is a multi-line comment block. This is useful for long
</span><span style="color:#75715e"> comments that span several lines.
</span><span style="color:#75715e"> */</span>


</code></pre></div><h3 id="数据类型">数据类型</h3>
<p>Dart 是<em>静态类型的</em>，这意味着 Dart 中的每个变量都有一个在编译代码时必须知道的类型。运行程序时变量类型不能改变。C、Java、Swift 和 Kotlin 也是静态类型的。</p>
<p>这与<em>动态类型的</em>Python 和 JavaScript 等语言形成对比。这意味着在运行程序时变量可以保存不同类型的数据。编译代码时不需要知道类型。</p>
<p>单击<code>myAge</code>编辑器窗口并查看<em>文档</em>面板。你会看到 Dart<em>推断出</em>它<code>myAge</code>是一个，<code>int</code>因为它是用整数值<em>35</em>初始化的。</p>
<p>如果您没有明确指定数据类型，Dart 会使用<em>类型推断</em>来尝试确定它，就像 Swift 和 Kotlin 所做的那样。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/type_inference.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/type_inference-650x339.png">
            <img class="mx-auto" alt="类型推断" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/type_inference-650x339.png" />
        </a>
    </a></p>
<p>Dart 还对<code>int</code>. 输入一个变量，<code>pi</code>，等于 3.14：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> pi <span style="color:#f92672">=</span> <span style="color:#ae81ff">3.14</span>;

print(pi); <span style="color:#75715e">// 3.14
</span><span style="color:#75715e"></span>

</code></pre></div><p>Dart 推断<code>pi</code>为 a 是<code>double</code>因为您使用了一个浮点值来初始化它。您可以在 Dart 信息面板中通过单击 来验证<code>pi</code>。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/pi_output.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/pi_output-650x339.png">
            <img class="mx-auto" alt="双数据类型推断" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/pi_output-650x339.png" />
        </a>
    </a></p>
<p>或者，您可以声明类型，而不是使用类型推断。</p>
<h3 id="基本dart类型">基本dart类型</h3>
<p>Dart 使用以下基本类型：</p>
<ul>
<li><em>int</em> : 整数</li>
<li><em>double</em> : 浮点数</li>
<li><em>bool</em> : 布尔值</li>
<li><em>字符串</em>：字符序列</li>
</ul>
<p>以下是每种 Dart 类型的示例：</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2020/08/datatypes.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/datatypes-650x314.png">
            <img class="mx-auto" alt="Dart 数据类型" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/datatypes-650x314.png" />
        </a>
    </a></p>
<p><code>int</code>并且<code>double</code>都派生自一个名为<code>num</code>. <code>num</code>使用<code>dynamic</code>关键字来模拟静态类型 Dart 中的动态类型。</p>
<p>通过替换<code>var</code>为您要使用的类型来执行此操作：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">int</span> yourAge <span style="color:#f92672">=</span> <span style="color:#ae81ff">27</span>;

print(yourAge); <span style="color:#75715e">// 27
</span><span style="color:#75715e"></span>

</code></pre></div><h3 id="动态关键字">动态关键字</h3>
<p>如果您使用<code>dynamic</code>关键字而不是<code>var</code>，您将获得有效的动态类型变量：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">dynamic</span> numberOfKittens;


</code></pre></div><p>在这里，您可以设置<code>numberOfKittens</code>为<code>String</code>使用引号。您将在本教程的后面了解有关<code>String</code>该类型的更多信息。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">numberOfKittens <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;There are no kittens!&#39;</span>;

print(numberOfKittens); <span style="color:#75715e">// There are no kittens!
</span><span style="color:#75715e"></span>

</code></pre></div><p><code>numberOfKittens</code>有一个类型，因为 Dart 有静态类型。但该类型是<code>dynamic</code>，这意味着您可以为它分配具有其他类型的其他值。因此，您可以<code>int</code>在打印语句下方分配一个值。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">numberOfKittens <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;

print(numberOfKittens); <span style="color:#75715e">// 0
</span><span style="color:#75715e"></span>

</code></pre></div><p>或者，如果你在<a href="https://en.wikipedia.org/wiki/Schr%C3%B6dinger's_cat">薛定谔的盒子里</a>有一只小猫，你可以赋值<code>double</code>：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">numberOfKittens <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.5</span>;

print(numberOfKittens); <span style="color:#75715e">// 0.5
</span><span style="color:#75715e"></span>

</code></pre></div><p><a href="https://koenig-media.raywenderlich.com/uploads/2020/08/schrodingers-cat.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/schrodingers-cat-650x437.png">
            <img class="mx-auto" alt="薛定谔猫" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/schrodingers-cat-650x437.png" />
        </a>
    </a></p>
<p>单击<em>运行</em>以查看<code>numberOfKittens</code>控制台中打印的三个不同值。在每种情况下， 的类型<code>numberOfKittens</code>仍然存在<code>dynamic</code>，即使变量本身持有不同类型的值。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/dynamic_data_type.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dynamic_data_type-650x239.png">
            <img class="mx-auto" alt="Dart 动态数据类型" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dynamic_data_type-650x239.png" />
        </a>
    </a></p>
<h3 id="布尔值">布尔值</h3>
<p>该<code>bool</code>类型包含<code>true</code>或 的值<code>false</code>。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">bool</span> areThereKittens <span style="color:#f92672">=</span> <span style="color:#66d9ef">false</span>;

print(areThereKittens); <span style="color:#75715e">// false
</span><span style="color:#75715e"></span>

</code></pre></div><p>但是，如果你看看薛定谔的盒子，你可能会转而养一只活生生的小猫：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">bool</span> areThereKittens <span style="color:#f92672">=</span> <span style="color:#66d9ef">false</span>;

print(areThereKittens); <span style="color:#75715e">// false
</span><span style="color:#75715e"></span>

</code></pre></div><p>再次运行代码以在控制台中查看您的布尔值。你在盒子里看是一件好事！:]</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/bool_data_type.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/bool_data_type-650x276.png">
            <img class="mx-auto" alt="Dart bool 数据类型" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/bool_data_type-650x276.png" />
        </a>
    </a></p>
<h2 id="操作符">操作符</h2>
<p>Dart 具有您在其他语言(如 C、Swift 和 Kotlin)中熟悉的所有常用运算符。</p>
<p>Dart 运算符的一些示例包括：</p>
<ul>
<li>算术</li>
<li>平等</li>
<li>递增和递减</li>
<li>比较</li>
<li>合乎逻辑的</li>
</ul>
<p><em>注意</em>：Dart 还允许<em>运算符重载</em>，就像在 C++ 和 Kotlin 中一样，但这超出了本教程的范围。要了解有关该主题的更多信息，请访问 Wikipedia 的<a href="https://en.wikipedia.org/wiki/Operator_overloading">overator 重载</a>页面。</p>
<p>接下来，您将了解这些运算符中的每一个。</p>
<h3 id="算术运算符">算术运算符</h3>
<p>算术运算符的工作方式与您期望的一样。通过向 DartPad 添加一系列操作来尝试它们：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">print(<span style="color:#ae81ff">40</span> <span style="color:#f92672">+</span> <span style="color:#ae81ff">2</span>); <span style="color:#75715e">// 42
</span><span style="color:#75715e"></span>
print(<span style="color:#ae81ff">44</span> <span style="color:#f92672">-</span> <span style="color:#ae81ff">2</span>); <span style="color:#75715e">// 42
</span><span style="color:#75715e"></span>
print(<span style="color:#ae81ff">21</span> <span style="color:#f92672">*</span> <span style="color:#ae81ff">2</span>); <span style="color:#75715e">// 42
</span><span style="color:#75715e"></span>
print(<span style="color:#ae81ff">84</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">2</span>); <span style="color:#75715e">// 42.0
</span><span style="color:#75715e"></span>

</code></pre></div><p>对于除法，即使是整数，Dart 也会将结果变量推断为 a <code>double</code>。这就是为什么最后一条语句得到<em>42.0</em>而不是 42的原因<code>print</code>。</p>
<p><em>注意</em>：DartPad 在控制台中将“84 / 2”的结果显示为 42，因为它将输出到控制台的格式设置为仅显示有效数字。如果您在 Dart SDK 的 Dart 程序中打印相同的语句，您将得到<em>42.0</em>作为结果。</p>
<h3 id="等号运算符">等号运算符</h3>
<p>Dart 使用<em>双等于</em>( <code>==</code>) 等于和<em>不等于</em>( <code>!=</code>) 运算符：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">print(<span style="color:#ae81ff">42</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">43</span>); <span style="color:#75715e">// false
</span><span style="color:#75715e"></span>
print(<span style="color:#ae81ff">42</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">43</span>); <span style="color:#75715e">// true
</span><span style="color:#75715e"></span>

</code></pre></div><h3 id="比较运算符">比较运算符</h3>
<p>Dart 使用典型的比较运算符：</p>
<ul>
<li>小于 (&lt;)</li>
<li>大于 (&gt;)</li>
<li>等于 (=&gt;)</li>
</ul>
<p>这里有些例子：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">print(<span style="color:#ae81ff">42</span> <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">43</span>); <span style="color:#75715e">// true print(42 &gt;= 43); // false
</span><span style="color:#75715e"></span>

</code></pre></div><p>此外，它还使用常用的复合算术/赋值运算符：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> value <span style="color:#f92672">=</span> <span style="color:#ae81ff">42.0</span>;

value <span style="color:#f92672">+=</span> <span style="color:#ae81ff">1</span>; print(value); <span style="color:#75715e">// 43.0
</span><span style="color:#75715e"></span>
value <span style="color:#f92672">-=</span> <span style="color:#ae81ff">1</span>; print(value); <span style="color:#75715e">// 42.0
</span><span style="color:#75715e"></span>
value <span style="color:#f92672">*=</span> <span style="color:#ae81ff">2</span>; print(value); <span style="color:#75715e">// 84.0
</span><span style="color:#75715e"></span>
value <span style="color:#f92672">/=</span> <span style="color:#ae81ff">2</span>; print(value); <span style="color:#75715e">// 42.0
</span><span style="color:#75715e"></span>

</code></pre></div><p>复合算术/赋值运算符执行两项任务。<code>+=</code>将右边的值与左边的变量相加，然后将结果赋给变量。</p>
<p>的缩写形式<code>+= 1</code>是<code>++</code>：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">value<span style="color:#f92672">++</span>;

print(value); <span style="color:#75715e">// 43.0
</span><span style="color:#75715e"></span>

</code></pre></div><p>Dart 有通常的模运算符 ( <code>%</code>) 来返回一个数除以另一个数后的余数：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">print(<span style="color:#ae81ff">392</span> <span style="color:#f92672">%</span> <span style="color:#ae81ff">50</span>); <span style="color:#75715e">// 42
</span><span style="color:#75715e"></span>

</code></pre></div><p>392 ÷ 50 = 7.84，但结果窗格中的 42 来自哪里？用长除法更容易看到。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/06/long_division_392_50.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/long_division_392_50.png">
            <img class="mx-auto" alt="长除法显示 392 除以 50，余数为 42" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/long_division_392_50.png" />
        </a>
    </a></p>
<h3 id="逻辑运算符">逻辑运算符</h3>
<p>DART使用相同的逻辑运算符为其他语言，包括<code>&amp;&amp;</code>了<em>和</em>和<code>||</code>的<em>OR</em>。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">print((<span style="color:#ae81ff">41</span> <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">42</span>) <span style="color:#f92672">&amp;&amp;</span> (<span style="color:#ae81ff">42</span> <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">43</span>)); <span style="color:#75715e">// true
</span><span style="color:#75715e"></span>
print((<span style="color:#ae81ff">41</span> <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">42</span>) <span style="color:#f92672">||</span> (<span style="color:#ae81ff">42</span> <span style="color:#f92672">&gt;</span> <span style="color:#ae81ff">43</span>)); <span style="color:#75715e">// true
</span><span style="color:#75715e"></span>

</code></pre></div><p>否定运算符是<em>感叹号</em>，它把假变成真，真变成假。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">print(<span style="color:#f92672">!</span>(<span style="color:#ae81ff">41</span> <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">42</span>)); <span style="color:#75715e">// false
</span><span style="color:#75715e"></span>

</code></pre></div><p>有关<a href="https://dart.dev/guides/language/language-tour#operators">dart运算符</a>的完整列表，请参阅 Dart 文档。</p>
<h2 id="字符串">字符串</h2>
<p>Dart 字符串类型是<code>String</code>. 字符串在 Dart 中使用由<em>单**引号</em>或<em>双引号括起来的</em>文本表示。</p>
<p>您可以使用<code>var</code>and 类型推断或<code>String</code>创建字符串变量，就像您见过的其他类型一样：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> firstName <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;Albert&#39;</span>;

<span style="color:#66d9ef">String</span> lastName <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Einstein&#34;</span>;


</code></pre></div><p>与 Kotlin 和 Swift 等语言类似，您可以使用美元符号将表达式的值嵌入到字符串中：<em>${ *expression* }</em>。</p>
<p>如果表达式是标识符，则可以省略*{ }*。添加以下内容：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> physicist <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;</span><span style="color:#e6db74">$</span>firstName<span style="color:#e6db74"> </span><span style="color:#e6db74">$</span>lastName<span style="color:#e6db74"> likes the number </span><span style="color:#e6db74">${</span><span style="color:#ae81ff">84</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">2</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>;

print(physicist); <span style="color:#75715e">// Albert Einstein
</span><span style="color:#75715e"></span>

</code></pre></div><p><code>$firstName</code>和<code>$lastName</code>由可变值替换。在``返回计算的结果。</p>
<h3 id="转义字符串">转义字符串</h3>
<p>Dart 中使用的转义序列类似于其他类 C 语言中使用的转义序列。例如，您用于<code>\n</code>换行。</p>
<p>如果字符串中有特殊字符，请使用<code>\</code>转义它们：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> quote <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;If you can</span><span style="color:#ae81ff">\&#39;</span><span style="color:#e6db74">t explain it simply</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">you don</span><span style="color:#ae81ff">\&#39;</span><span style="color:#e6db74">t understand it well enough.&#39;</span>;

print(quote);

<span style="color:#75715e">// If you can&#39;t explain it simply
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// you don&#39;t understand it well enough.
</span><span style="color:#75715e"></span>

</code></pre></div><p>此示例使用单引号，因此它需要一个转义序列 ,<code>\'</code>将<em>can&rsquo;t</em>和<em>don&rsquo;t</em>的撇号嵌入到字符串中。如果您改用双引号，则不需要转义撇号。</p>
<p>如果您需要在字符串中显示转义序列，您可以使用以 为前缀的<em>原始字符串</em><code>r</code>。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> rawString <span style="color:#f92672">=</span> <span style="color:#e6db74">r&#34;If you can&#39;t explain it simply\nyou don&#39;t understand it well enough.&#34;</span>;

print(rawString); 

<span style="color:#75715e">// If you can&#39;t explain it simply\nyou don&#39;t understand it well enough.
</span><span style="color:#75715e"></span>

</code></pre></div><p>在这里，Dart 将其<code>\n</code>视为普通文本，因为字符串以<code>r</code>.</p>
<p>单击DartPad 中的<em>RUN</em>以在控制台中查看所有字符串。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/String_data_type.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/String_data_type-650x276.png">
            <img class="mx-auto" alt="Dart 字符串数据类型" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/String_data_type-650x276.png" />
        </a>
    </a></p>
<h2 id="不变性">不变性</h2>
<p>Dart 使用关键字<code>const</code>和<code>final</code>不改变的值。</p>
<p>使用<code>const</code>对于那些在编译时已知值。使用<code>final</code>了不具备在编译时已知的，但初始化后不能被重新分配的值。</p>
<p><em>注意</em>：<code>final</code>就像<code>val</code>在 Kotlin 或<code>let</code>Swift 中一样。</p>
<p>您可以使用<code>const</code>and<code>final</code>代替<code>var</code>并让类型推断确定类型：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">const</span> speedOfLight <span style="color:#f92672">=</span> <span style="color:#ae81ff">299792458</span>;

print(speedOfLight); <span style="color:#75715e">// 299792458
</span><span style="color:#75715e"></span>

</code></pre></div><p>在这里，Dart 推断这<code>speedOfLight</code>是一个<code>int</code>，正如您在 DartPad 的信息面板中看到的那样。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/const_inference.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/const_inference-650x239.png">
            <img class="mx-auto" alt="Dart 常量推断" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/const_inference-650x239.png" />
        </a>
    </a></p>
<p><code>final</code>表示变量是<em>不可变的</em>，这意味着您不能重新分配<code>final</code>值。您可以使用<code>final</code>或显式声明类型<code>const</code>：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">final</span> planet <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;Jupiter&#39;</span>;

<span style="color:#75715e">// planet = &#39;Mars&#39;;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// error: planet can only be set once
</span><span style="color:#75715e"></span>
<span style="color:#66d9ef">final</span> <span style="color:#960050;background-color:#1e0010">​</span><span style="color:#66d9ef">String</span> moon <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;Europa&#39;</span>;

print(<span style="color:#e6db74">&#39;</span><span style="color:#e6db74">$</span>planet<span style="color:#e6db74"> has a moon, </span><span style="color:#e6db74">$</span>moon<span style="color:#e6db74">&#39;</span>);

<span style="color:#75715e">// Jupiter has a moon, Europa
</span></code></pre></div><h2 id="可空性">可空性</h2>
<p>过去，如果你没有初始化一个变量，Dart 会给它赋值<code>null</code>，这意味着变量中<em>没有</em>存储<em>任何内容</em>。不过，从 Dart 2.12 开始，Dart 加入了其他语言，如 Swift 和 Kotlin，默认情况下不可为空。</p>
<p>此外，Dart<em>保证</em>不可为空的类型永远不会包含空值。这被称为<a href="https://dart.dev/null-safety"><em>空安全</em></a>。</p>
<p>通常，如果要声明一个变量，必须对其进行初始化：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">String</span> middleName <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;May&#39;</span>;

print(middleName); <span style="color:#75715e">// May
</span><span style="color:#75715e"></span>

</code></pre></div><p>但是，并非每个人都有中间名，因此创建<code>middleName</code>一个可为空的值是有意义的。要告诉 Dart 您想要允许 value <code>null</code>，请<code>?</code>在类型后添加。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">String</span><span style="color:#f92672">?</span> middleName <span style="color:#f92672">=</span> <span style="color:#66d9ef">null</span>;

print(middleName); <span style="color:#75715e">// null
</span><span style="color:#75715e"></span>

</code></pre></div><p>可空类型的默认值是<code>null</code>，因此您可以将表达式简化为以下内容：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">String</span><span style="color:#f92672">?</span> middleName;

print(middleName); <span style="color:#75715e">// null
</span><span style="color:#75715e"></span>

</code></pre></div><p>运行它并<code>null</code>打印到控制台。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/null_string_output.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/null_string_output-650x239.png">
            <img class="mx-auto" alt="Dart 空字符串输出" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/null_string_output-650x239.png" />
        </a>
    </a></p>
<h3 id="空感知运算符">空感知运算符</h3>
<p>Dart 有一些可<em>识别空值的</em>运算符，您可以在处理空值时使用。</p>
<p>双问号运算符 ，<code>??</code>就像Kotlin 中的<em>Elvis 运算符</em>：如果对象不为空，它返回左侧操作数。否则，它返回右边的值：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> name <span style="color:#f92672">=</span> middleName <span style="color:#f92672">??</span> <span style="color:#e6db74">&#39;none&#39;</span>;

print(name); <span style="color:#75715e">// none
</span><span style="color:#75715e"></span>

</code></pre></div><p>由于<code>middleName</code>is <code>null</code>，Dart 分配 的右侧值<code>'none'</code>。</p>
<p>该<code>?.运算符保护您访问空对象的属性。</code>null`如果对象本身为空，则返回。否则，它返回右侧属性的值：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">print(middleName<span style="color:#f92672">?</span>.length); <span style="color:#75715e">// null
</span><span style="color:#75715e"></span>
</code></pre></div><p>在空安全之前的日子里，如果你忘记了问号并写了<code>middleName.length</code>，如果<code>middleName</code>为空，你的应用程序会在运行时崩溃。这不再是问题，因为 Dart 现在会立即告诉您何时需要处理空值。</p>
<h2 id="控制流">控制流</h2>
<p><em>控制流</em>可让您决定何时执行、跳过或重复某些代码行。您可以使用<em>条件</em>和<em>循环</em>来处理 Dart 中的控制流。</p>
<p>在本节中，您将了解更多关于：</p>
<ul>
<li>条件句</li>
<li>While 循环</li>
<li>继续和中断</li>
<li>For 循环</li>
</ul>
<p>以下是您需要了解的有关 Dart 中的控制流元素的信息。</p>
<h2 id="条件句">条件句</h2>
<p>控制流的最基本形式是根据程序运行时发生的条件决定是执行还是跳过代码的某些部分。</p>
<p>处理条件的语言结构是<code>if</code>/<code>else</code>语句。<code>if</code>/<code>else</code>在 Dart 中看起来几乎与它在其他类 C 语言中的使用相同。</p>
<h3 id="如果语句">如果语句</h3>
<p>假设您有一个变量，<code>animal</code>，它当前是一只狐狸。它看起来像这样：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> animal <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;fox&#39;</span>;

</code></pre></div><p><a href="https://koenig-media.raywenderlich.com/uploads/2020/08/creature-head-fox.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/creature-head-fox-500x500.png">
            <img class="mx-auto" alt="狐狸" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/creature-head-fox-500x500.png" />
        </a>
    </a></p>
<p>您可以使用<code>if</code>语句来检查<code>animal</code>是猫还是狗，然后运行一些相应的代码。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">if</span> (animal <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;cat&#39;</span> <span style="color:#f92672">||</span> animal <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;dog&#39;</span>) {
  print(<span style="color:#e6db74">&#39;Animal is a house pet.&#39;</span>);
}

</code></pre></div><p>在这里，您使用了<em>相等</em>和<em>OR</em>运算符<code>bool</code>为<code>if</code>语句创建了一个内部条件。</p>
<h3 id="其他语句">其他语句</h3>
<p>使用<code>else</code>子句，如果条件为假，您可以运行替代代码：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">else</span> {
  print(<span style="color:#e6db74">&#39;Animal is NOT a house pet.&#39;</span>);
}
<span style="color:#75715e">// Animal is NOT a house pet.
</span><span style="color:#75715e"></span>
</code></pre></div><p>您也可以将多个组合<code>if</code>/<code>else</code>语句成<code>if</code>/ <code>else if</code>/<code>else</code>结构：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">if</span> (animal <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;cat&#39;</span> <span style="color:#f92672">||</span> animal <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;dog&#39;</span>) {
  print(<span style="color:#e6db74">&#39;Animal is a house pet.&#39;</span>);
} <span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span> (animal <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;rhino&#39;</span>) {
  print(<span style="color:#e6db74">&#39;That</span><span style="color:#ae81ff">\&#39;</span><span style="color:#e6db74">s a big animal.&#39;</span>);
} <span style="color:#66d9ef">else</span> {
  print(<span style="color:#e6db74">&#39;Animal is NOT a house pet.&#39;</span>);
}
<span style="color:#75715e">// Animal is NOT a house pet.
</span><span style="color:#75715e"></span>
</code></pre></div><p>你可以有很多<code>else if</code>分支之间<code>if</code>，并<code>else</code>根据您的需要。</p>
<h2 id="while-循环">While 循环</h2>
<p>循环允许您重复代码一定次数或基于特定条件。您可以使用<em>while 循环</em>处理基于条件的重复。</p>
<p>Dart 中有两种形式的 while 循环：<code>while</code>和<code>do-while</code>。不同之处在于 for <code>while</code>，循环条件发生在代码块之前。在<code>do-while</code>，条件发生在之后。这意味着<code>do-while</code>循环确保代码块至少运行一次。</p>
<h3 id="测试-while-循环">测试 While 循环</h3>
<p>要尝试此操作，请创建一个<code>i</code>初始化为 1的变量：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>;

</code></pre></div><p>接下来，使用<code>while</code>循环<code>i</code>在递增时打印。设置条件<code>i</code>为小于10：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">while</span> (i <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">10</span>) {
  print(i);
  i<span style="color:#f92672">++</span>;
}
<span style="color:#75715e">// 1
</span><span style="color:#75715e">// 2
</span><span style="color:#75715e">// 3
</span><span style="color:#75715e">// 4
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// 6
</span><span style="color:#75715e">// 7
</span><span style="color:#75715e">// 8
</span><span style="color:#75715e">// 9
</span><span style="color:#75715e"></span>
</code></pre></div><p>运行代码，您将看到<code>while</code>循环打印数字 1 到 9。</p>
<h3 id="尝试-do-while-循环">尝试 Do-While 循环</h3>
<p><code>i</code>在 DartPad 中重置，然后添加一个<code>do-while</code>循环：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">i <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>;
<span style="color:#66d9ef">do</span> {
  print(i);
  i<span style="color:#f92672">++</span>;
} <span style="color:#66d9ef">while</span> (i <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">10</span>);
<span style="color:#75715e">// 1
</span><span style="color:#75715e">// 2
</span><span style="color:#75715e">// 3
</span><span style="color:#75715e">// 4
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// 6
</span><span style="color:#75715e">// 7
</span><span style="color:#75715e">// 8
</span><span style="color:#75715e">// 9
</span><span style="color:#75715e"></span>
</code></pre></div><p>结果和以前一样。然而这一次，循环体在检查循环退出条件之前运行了一次。</p>
<h2 id="继续和中断">继续和中断</h2>
<p>Dart在循环和其他地方使用<code>continue</code>和<code>break</code>关键字。以下是他们所做的：</p>
<ul>
<li><em>continue</em>：跳过循环中剩余的代码并立即进入下一次迭代。</li>
<li><em>break</em>：停止循环并在循环体之后继续执行。</li>
</ul>
<p><code>continue</code>在代码中使用时要小心。例如，如果您<code>do-while</code>从上面进行循环，并且您希望在<code>i</code>等于 5时继续，则可能会导致*无限循环，*具体取决于您放置<code>continue</code>语句的位置：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">i <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>;
<span style="color:#66d9ef">do</span> {
  print(i);
  <span style="color:#66d9ef">if</span> (i <span style="color:#f92672">==</span> <span style="color:#ae81ff">5</span>) {
    <span style="color:#66d9ef">continue</span>;
  }            
  <span style="color:#f92672">++</span>i;
} <span style="color:#66d9ef">while</span> (i <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">10</span>);
<span style="color:#75715e">// 1
</span><span style="color:#75715e">// 2
</span><span style="color:#75715e">// 3
</span><span style="color:#75715e">// 4
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e">// ...
</span><span style="color:#75715e"></span>
</code></pre></div><p>发生无限循环的原因是，一旦<code>i</code>为 5，您就不会再增加它，因此条件始终为真。</p>
<p>如果在 DartPad 中运行它，无限循环将导致浏览器挂起。相反，使用<code>break</code>，因此循环在<code>i</code>达到 5后结束：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">i <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>;
<span style="color:#66d9ef">do</span> {
  print(i);
  <span style="color:#66d9ef">if</span> (i <span style="color:#f92672">==</span> <span style="color:#ae81ff">5</span>) {
    <span style="color:#66d9ef">break</span>;
  }
  <span style="color:#f92672">++</span>i;
} <span style="color:#66d9ef">while</span> (i <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">10</span>);
<span style="color:#75715e">// 1
</span><span style="color:#75715e">// 2
</span><span style="color:#75715e">// 3
</span><span style="color:#75715e">// 4
</span><span style="color:#75715e">// 5
</span><span style="color:#75715e"></span>
</code></pre></div><p>运行代码。现在，循环在五次迭代后结束。</p>
<h2 id="for-循环">For 循环</h2>
<p>在 Dart 中，您使用<code>for</code>循环来循环预定次数。<code>for</code>循环由初始化、循环条件和动作组成。再一次，它们类似于<code>for</code>其他语言中的循环。</p>
<p>Dart 还提供了一个<code>for-in</code>循环，它遍历一组对象。稍后您将了解有关这些的更多信息。</p>
<p>要查看<code>for</code>循环的工作原理，请为总和创建一个变量：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> sum <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;

</code></pre></div><p>接下来，使用<code>for</code>循环将循环计数器从<code>i</code>1初始化。然后您将检查它<code>i</code>是否小于或等于 10，并<code>i</code>在每次循环后递增。</p>
<p>在循环内，使用复合赋值添加<code>i</code>到运行总和：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">var</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>; i <span style="color:#f92672">&lt;=</span> <span style="color:#ae81ff">10</span>; i<span style="color:#f92672">++</span>) {
  sum <span style="color:#f92672">+=</span> i;  
}
print(<span style="color:#e6db74">&#34;The sum is </span><span style="color:#e6db74">$</span>sum<span style="color:#e6db74">&#34;</span>); <span style="color:#75715e">// The sum is 55
</span><span style="color:#75715e"></span>
</code></pre></div><p>在 DartPad 中运行您的代码以查看总和。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/dart_for_loop.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_for_loop-650x221.png">
            <img class="mx-auto" alt="Dart for 循环" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_for_loop-650x221.png" />
        </a>
    </a></p>
<h1 id="collections">Collections</h1>
<p>集合对于对相关数据进行分组很有用。Dart 包括几种不同类型的集合，但本教程将介绍两种最常见的：<code>List</code>和<code>Map</code>.</p>
<h2 id="列表">列表</h2>
<p>Dart 中的<em>列表</em>类似于其他语言中的<em>数组</em>。您可以使用它们来维护有序的值列表。列表是从零开始的，因此列表中的第一项位于索引 0 处：</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/dart_list.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_list-650x166.png">
            <img class="mx-auto" alt="甜点清单" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_list-650x166.png" />
        </a>
    </a></p>
<p>以下是不同甜点的清单：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">List desserts <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;cookies&#39;</span>, <span style="color:#e6db74">&#39;cupcakes&#39;</span>, <span style="color:#e6db74">&#39;donuts&#39;</span>, <span style="color:#e6db74">&#39;pie&#39;</span>];

</code></pre></div><p>您将列表的元素括在方括号中：<code>[ ]</code>。使用逗号分隔元素。</p>
<p>在该行的开头，您可以看到类型为<code>List</code>。您会注意到没有包含类型。Dart 推断该列表具有类型。<code>List</code></p>
<p>这是一个整数列表：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">final</span> numbers <span style="color:#f92672">=</span> [<span style="color:#ae81ff">42</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">299792458</span>, <span style="color:#ae81ff">100</span>];

</code></pre></div><p>单击<code>numbers</code>DartPad，您将看到 Dart 将类型识别为 a <code>List</code>of <code>int</code>。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/list_of_integers.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/list_of_integers-650x221.png">
            <img class="mx-auto" alt="int的飞镖列表" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/list_of_integers-650x221.png" />
        </a>
    </a></p>
<h3 id="使用列表元素">使用列表元素</h3>
<p>要访问列表的元素，请使用<em>下标表示法</em>，将索引号放在列表变量名称之后的方括号之间。例如：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">final</span> firstDessert <span style="color:#f92672">=</span> desserts[<span style="color:#ae81ff">0</span>];
print(firstDessert); <span style="color:#75715e">// cookies
</span><span style="color:#75715e"></span>
</code></pre></div><p>由于列表索引是从零开始的，因此<code>desserts[0]</code>是列表的第一个元素。</p>
<p>分别使用<code>add</code>和 来添加和删除元素<code>remove</code>：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">desserts.add(<span style="color:#e6db74">&#39;cake&#39;</span>);
print(desserts); 
<span style="color:#75715e">// [cookies, cupcakes, donuts, pie, cake]
</span><span style="color:#75715e"></span>
desserts.remove(<span style="color:#e6db74">&#39;donuts&#39;</span>);
print(desserts); 
<span style="color:#75715e">// [cookies, cupcakes, pie, cake]
</span><span style="color:#75715e"></span>
</code></pre></div><p>运行代码查看结果。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/dart_list_methods.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_list_methods-650x250.png">
            <img class="mx-auto" alt="Dart 列表添加和删除方法" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_list_methods-650x250.png" />
        </a>
    </a></p>
<p>早些时候，您了解了<code>for</code>循环。Dart 的<code>for-in</code>循环尤其适用于列表。试试看：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">final</span> dessert <span style="color:#66d9ef">in</span> desserts) {
  print(<span style="color:#e6db74">&#39;I love to eat </span><span style="color:#e6db74">$</span>dessert<span style="color:#e6db74">.&#39;</span>);
}
<span style="color:#75715e">// I love to eat cookies.
</span><span style="color:#75715e">// I love to eat cupcakes.
</span><span style="color:#75715e">// I love to eat pie.
</span><span style="color:#75715e">// I love to eat cake.
</span><span style="color:#75715e"></span>
</code></pre></div><p>您不需要使用索引。Dart 只是遍历 的每个元素<code>desserts</code>并将其每次分配给名为 的变量<code>dessert</code>。</p>
<p>饿了？好吧，在你吃完蔬菜之前，你不能吃任何甜点。:]</p>
<h2 id="map">map</h2>
<p>当你想要一个配对值列表时，<code>Map</code>是一个不错的选择。Dart<code>Map</code>类似于Swift 中的<em>字典</em>和Kotlin 中的<em>映射</em>。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/dart_maps.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_maps-650x347.png">
            <img class="mx-auto" alt="飞镖地图" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_maps-650x347.png" />
        </a>
    </a></p>
<p>以下是 Dart 中的地图示例：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">Map<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">String</span>, <span style="color:#66d9ef">int</span><span style="color:#f92672">&gt;</span> calories <span style="color:#f92672">=</span> {
  <span style="color:#e6db74">&#39;cake&#39;</span><span style="color:#f92672">:</span> <span style="color:#ae81ff">500</span>,
  <span style="color:#e6db74">&#39;donuts&#39;</span><span style="color:#f92672">:</span> <span style="color:#ae81ff">150</span>,
  <span style="color:#e6db74">&#39;cookies&#39;</span><span style="color:#f92672">:</span> <span style="color:#ae81ff">100</span>,
};

</code></pre></div><p>您围绕<code>Map</code>s的花括号<code>{ }</code>。使用逗号分隔地图的元素。</p>
<p>映射的元素称为<em>键值对</em>，其中键位于冒号左侧，值位于右侧。</p>
<p>您可以通过使用键查找值来查找值，如下所示：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">final</span> donutCalories <span style="color:#f92672">=</span> calories[<span style="color:#e6db74">&#39;donuts&#39;</span>];
print(donutCalories); <span style="color:#75715e">// 150
</span><span style="color:#75715e"></span>
</code></pre></div><p>键 ,<code>'donuts'</code>位于地图名称后面的方括号内。在这种情况下，它映射到 的值<code>150</code>。</p>
<p>单击<code>donutCalories</code>DartPad，您将看到推断的类型是<code>int?</code>而不是<code>int</code>。这是因为，如果地图不包含您要查找的键，它将返回一个<code>null</code>值。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/dart_map_output.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_map_output-650x230.png">
            <img class="mx-auto" alt="飞镖图输出" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_map_output-650x230.png" />
        </a>
    </a></p>
<p>通过指定键并为其分配值来向映射添加新元素：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">calories[<span style="color:#e6db74">&#39;brownieFudgeSundae&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">1346</span>;
print(calories);
<span style="color:#75715e">// {cake: 500, donuts: 150, cookies: 100, brownieFudgeSundae: 1346}
</span><span style="color:#75715e"></span>
</code></pre></div><p>运行该代码，您将看到以水平格式打印的地图，最后是您的新甜点。</p>
<h1 id="functions">Functions</h1>
<p>函数使您可以将多行相关代码打包到一个主体中。然后调用该函数以避免在整个 Dart 应用程序中重复这些代码行。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/dart_function.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_function-650x259.png">
            <img class="mx-auto" alt="飞镖功能" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_function-650x259.png" />
        </a>
    </a></p>
<p>一个函数由以下元素组成：</p>
<ul>
<li>返回类型</li>
<li>函数名</li>
<li>括号中的参数列表</li>
<li>括在括号中的函数体</li>
</ul>
<h3 id="定义函数">定义函数</h3>
<p>您要转换为函数的代码位于大括号内。调用函数时，传入与函数<em>参数</em>类型匹配的参数。</p>
<p>接下来，您将在 DartPad 中编写一个新函数，该函数将检查给定的字符串是否为<em>香蕉</em>：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">bool</span> isBanana(<span style="color:#66d9ef">String</span> fruit) {
  <span style="color:#66d9ef">return</span> fruit <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;banana&#39;</span>;
}

</code></pre></div><p>该函数用于<code>return</code>返回一个<code>bool</code>. 您传递给函数的参数决定了<code>bool</code>.</p>
<p>对于任何给定的输入，此函数将始终返回相同的值类型。如果函数不需要返回值，您可以将返回类型设置为<code>void</code>. <code>main</code>例如，这样做。</p>
<h3 id="使用函数">使用函数</h3>
<p>您可以通过传入字符串来调用该函数。然后，您可以将该调用的结果传递给<code>print</code>：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">void</span> main() {
  <span style="color:#66d9ef">var</span> fruit <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;apple&#39;</span>;
  print(isBanana(fruit)); <span style="color:#75715e">// false
</span><span style="color:#75715e"></span>}

</code></pre></div><h3 id="嵌套函数">嵌套函数</h3>
<p>通常，您可以在其他函数外部或 Dart 类内部定义函数。但是，您也可以嵌套 Dart 函数。例如，您可以嵌套<code>isBanana</code>在<code>main</code>.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">void</span> main() {
  <span style="color:#66d9ef">bool</span> isBanana(<span style="color:#66d9ef">String</span> fruit) {
    <span style="color:#66d9ef">return</span> fruit <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;banana&#39;</span>;
  }

  <span style="color:#66d9ef">var</span> fruit <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;apple&#39;</span>;
  print(isBanana(fruit)); <span style="color:#75715e">// false
</span><span style="color:#75715e"></span>}

</code></pre></div><p>您还可以将参数更改为函数，然后使用新参数再次调用它：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">fruit <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;banana&#39;</span>;
print(isBanana(fruit));  <span style="color:#75715e">// true
</span><span style="color:#75715e"></span>
</code></pre></div><p>调用函数的结果完全取决于你传入的参数。</p>
<h3 id="可选参数">可选参数</h3>
<p>如果函数的参数是可选的，您可以用方括号将其括起来并使类型可以为空：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">String</span> fullName( <span style="color:#66d9ef">String</span> first, <span style="color:#66d9ef">String</span> last, [ <span style="color:#66d9ef">String</span><span style="color:#f92672">?</span> title]) {
   <span style="color:#66d9ef">if</span> (title <span style="color:#f92672">==</span> <span style="color:#66d9ef">null</span> ) {
     <span style="color:#66d9ef">return</span>  <span style="color:#e6db74">&#39; </span><span style="color:#e6db74">$</span>first<span style="color:#e6db74">  </span><span style="color:#e6db74">$</span>last<span style="color:#e6db74"> &#39;</span> ; 
  } <span style="color:#66d9ef">else</span> {
     <span style="color:#66d9ef">return</span>  <span style="color:#e6db74">&#39; </span><span style="color:#e6db74">$</span>title<span style="color:#e6db74">  </span><span style="color:#e6db74">$</span>first<span style="color:#e6db74">  </span><span style="color:#e6db74">$</span>last<span style="color:#e6db74"> &#39;</span> ; 
  } 
}
</code></pre></div><p>在这个函数中，<code>title</code>是可选的。<code>null</code>如果您不指定它，它将默认为。</p>
<p>现在，您可以使用或不使用可选参数调用该函数：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">print(fullName(<span style="color:#e6db74">&#39;Joe&#39;</span>, <span style="color:#e6db74">&#39;Howard&#39;</span>));
<span style="color:#75715e">// Joe Howard
</span><span style="color:#75715e"></span>
print(fullName(<span style="color:#e6db74">&#39;Albert&#39;</span>, <span style="color:#e6db74">&#39;Einstein&#39;</span>, <span style="color:#e6db74">&#39;Professor&#39;</span>));
<span style="color:#75715e">// Professor Albert Einstein
</span><span style="color:#75715e"></span>
</code></pre></div><h3 id="命名参数和默认值">命名参数和默认值</h3>
<p>当您有多个参数时，很难记住哪个是哪个。飞镖解决了这一难题<em>命名参数</em>，您可以通过与周围大括号参数列表获得：<code>{ }</code>。</p>
<p>默认情况下，这些参数是可选的，但您可以为它们提供默认值或使用<code>required</code>关键字使它们成为必需：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">bool</span> withinTolerance({required <span style="color:#66d9ef">int</span> value, <span style="color:#66d9ef">int</span> min <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>, <span style="color:#66d9ef">int</span> max <span style="color:#f92672">=</span> <span style="color:#ae81ff">10</span>}) {
  <span style="color:#66d9ef">return</span> min <span style="color:#f92672">&lt;=</span> value <span style="color:#f92672">&amp;&amp;</span> value <span style="color:#f92672">&lt;=</span> max;
}

</code></pre></div><p><code>value</code>是必需的，而<code>min</code>和<code>max</code>是可选的，具有默认值。</p>
<p>使用命名参数，您可以通过提供带有冒号的参数名称以不同的顺序传递参数：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">print(withinTolerance(min: <span style="color:#ae81ff">1</span>, max: <span style="color:#ae81ff">5</span>, value: <span style="color:#ae81ff">11</span>)); <span style="color:#75715e">// false
</span><span style="color:#75715e"></span>
</code></pre></div><p>调用函数时，您可以不使用默认值的参数。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">print(withinTolerance(value: <span style="color:#ae81ff">5</span>)); <span style="color:#75715e">// true
</span><span style="color:#75715e"></span>
</code></pre></div><p>运行您的代码以查看正在运行的新函数。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/named_default_parameters.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/named_default_parameters-650x184.png">
            <img class="mx-auto" alt="命名和默认参数" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/named_default_parameters-650x184.png" />
        </a>
    </a></p>
<h3 id="匿名函数">匿名函数</h3>
<p>Dart 支持<em>一流的 functions</em>，这意味着它像对待任何其他数据类型一样对待函数。您可以将它们分配给变量，将它们作为参数传递并从其他函数中返回它们。</p>
<p>要将这些函数作为值传递，请省略函数名称和返回类型。由于没有名称，这种类型的函数称为<em>匿名函数</em>。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/dart_anonymous_function.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_anonymous_function-650x159.png">
            <img class="mx-auto" alt="匿名函数" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/dart_anonymous_function-650x159.png" />
        </a>
    </a></p>
<p>您可以将匿名函数分配给名为 的变量<code>onPressed</code>，如下所示：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">final</span> onPressed <span style="color:#f92672">=</span> () {
  print(<span style="color:#e6db74">&#39;button pressed&#39;</span>);
};

</code></pre></div><p><code>onPressed</code>有一个类型的值<code>Function</code>。空括号表示该函数没有参数。与常规函数一样，大括号内的代码是函数体。</p>
<p>要在函数体内执行代码，请调用变量名，就像它是函数名一样：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">oonPressed(); <span style="color:#75715e">// button pressed 按钮按下
</span></code></pre></div><p>您可以使用<em>箭头语法</em>简化函数体仅包含一行的函数。为此，请移除大括号并添加一个粗箭头<code>=&gt;</code>。</p>
<p>这是上面匿名函数和重构版本的比较：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#75715e">// original anonymous function
</span><span style="color:#75715e"></span><span style="color:#66d9ef">final</span> onPressed <span style="color:#f92672">=</span> () {
  print(<span style="color:#e6db74">&#39;button pressed&#39;</span>);
};

<span style="color:#75715e">// refactored
</span><span style="color:#75715e"></span><span style="color:#66d9ef">final</span> onPressed <span style="color:#f92672">=</span> () <span style="color:#f92672">=&gt;</span> print(<span style="color:#e6db74">&#39;button pressed&#39;</span>);

</code></pre></div><p>读起来好多了。</p>
<h3 id="使用匿名函数">使用匿名函数</h3>
<p>你会经常看到 Flutter 中的匿名函数，就像上面的那些，作为 UI 事件的回调传递。这使您可以指定在用户执行某些操作(例如按下按钮)时运行的代码。</p>
<p>您会看到匿名函数的另一个常见地方是集合。您可以为集合提供一个匿名函数，该函数将对集合的每个元素执行某些任务。例如：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#75715e">// 1
</span><span style="color:#75715e"></span><span style="color:#66d9ef">final</span> drinks <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;water&#39;</span>, <span style="color:#e6db74">&#39;juice&#39;</span>, <span style="color:#e6db74">&#39;milk&#39;</span>];
<span style="color:#75715e">// 2
</span><span style="color:#75715e"></span><span style="color:#66d9ef">final</span> bigDrinks <span style="color:#f92672">=</span> drinks.map(
  <span style="color:#75715e">// 3
</span><span style="color:#75715e"></span>  (drink) <span style="color:#f92672">=&gt;</span> drink.toUpperCase()
);
<span style="color:#75715e">// 4
</span><span style="color:#75715e"></span>print(bigDrinks); <span style="color:#75715e">// (WATER, JUICE, MILK)
</span><span style="color:#75715e"></span>
</code></pre></div><p>让我们看看每一步：</p>
<ol>
<li>定义一个<code>drinks</code>包含小写字母的列表。</li>
<li><code>.map</code> 获取所有列表值并返回一个带有它们的新集合。</li>
<li>匿名函数作为参数传递。在该匿名函数中，您有一个<code>drink</code>表示列表中每个元素的参数。</li>
<li>匿名函数的主体将每个元素转换为大写并返回值。由于原始列表是字符串列表，因此<code>drink</code>也有 type <code>String</code>。</li>
</ol>
<p>使用匿名函数并将其与它结合<code>.map</code>是将一个集合转换为另一个集合的便捷方式。</p>
<p><em>注意</em>：不要将<code>.map</code>方法与<code>Map</code>类型混淆。</p>
<p>运行代码以查看生成的集合。</p>
<p><a href="https://koenig-media.raywenderlich.com/uploads/2021/05/anonymous_functions-1.png">
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/image/anonymous_functions-1-650x248.png">
            <img class="mx-auto" alt="匿名函数" src="https://luckly007.oss-cn-beijing.aliyuncs.com/image/anonymous_functions-1-650x248.png" />
        </a>
    </a></p>
<p>恭喜，您已完成本教程。您现在应该对学习如何构建 Flutter 应用程序时看到的 Dart 代码有了更好的理解！</p>
<h2 id="然后去哪儿">然后去哪儿？</h2>
<p>另外，请查看<a href="https://dart.dev/guides">官方 Dart 文档</a>。</p>
<p>我们希望您喜欢这篇关于 Dart 基础知识的简短介绍。如果您有任何问题或意见，请关注我。</p>
<p>flutter</p>
<p>电子书  <a href="https://www.syncfusion.com/succinctly-free-ebooks/flutter-succinctly">https://www.syncfusion.com/succinctly-free-ebooks/flutter-succinctly</a></p>
<p>我计划出40集左右，给大家展示flutter基础使用</p>
<p><a href="https://kodestat.gitbook.io/flutter/">https://kodestat.gitbook.io/flutter/</a></p>
<p><a href="https://hackr.io/blog/how-to-learn-flutter">https://hackr.io/blog/how-to-learn-flutter</a></p>

        </div>

        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="https://luckly.work/">luckly</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="https://luckly.work/post/dart_basic/35%E5%88%86%E9%92%9F%E6%95%99%E4%BD%A0%E5%AD%A6%E4%BC%9Adart/">https://luckly.work/post/dart_basic/35%E5%88%86%E9%92%9F%E6%95%99%E4%BD%A0%E5%AD%A6%E4%BC%9Adart/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/post/flutter_basic/flutter%E5%86%85%E5%AD%98%E4%BC%98%E5%8C%96%E7%B3%BB%E5%88%97/">Flutter内存优化系列</a></li>
        
        <li><a href="/post/flutter_basic/ElementsKeys%E5%92%8CFlutter%E7%9A%84%E6%80%A7%E8%83%BD/">Elements，Keys和Flutter的性能</a></li>
        
        <li><a href="/post/flutter_basic/%E9%81%BF%E5%85%8D%E5%B0%86AnimationController%E4%B8%8EsetState%E4%B8%80%E8%B5%B7%E4%BD%BF%E7%94%A8/">避免将AnimationController与setState一起使用</a></li>
        
        <li><a href="/post/flutter_basic/%E5%A6%82%E4%BD%95%E4%BD%BF%E6%88%91%E4%BB%AC%E7%9A%84UI%E4%BB%A3%E7%A0%81%E6%9B%B4%E5%85%B7%E5%8F%AF%E8%AF%BB%E6%80%A7/">如何使我们的UI代码更具可读性</a></li>
        
        <li><a href="/post/flutter_basic/%E5%B0%86%E5%B0%8F%E9%83%A8%E4%BB%B6%E6%8B%86%E5%88%86%E4%B8%BA%E6%96%B9%E6%B3%95%E6%98%AF%E4%B8%80%E7%A7%8D%E5%8F%8D%E6%A8%A1%E5%BC%8F/">将小部件拆分为方法是一种反模式</a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            <ul class="clearfix">
                
                <li><a href='/tags/Flutter'>Flutter</a></li>
                
                <li><a href='/tags/dart'>dart</a></li>
                
            </ul>
            
        </div>
    </article>
    
    

    
    
    <div class="post bg-white">
      <script src="https://utteranc.es/client.js"
            repo= "https://github.com/ITmxs/repo"
            issue-term="pathname"
            theme="github-light"
            crossorigin="anonymous"
            async>
      </script>
    </div>
    
</div>

                    <footer id="footer">
    <div>
        &copy; 2021 <a href="https://luckly.work/">早起的年轻人 By luckly</a>
        
        | <a rel="nofollow" target="_blank" href="http://beian.miit.gov.cn/">粤ICP备2021号-1</a>
        
    </div>
    <br />
    <div>
        <div class="github-badge">
            <a href="https://juejin.cn/user/3843548384077192" target="_black" rel="nofollow"><span class="badge-subject">Powered by</span><span class="badge-value bg-blue">掘金</span></a>
        </div>
        <div class="github-badge">
            <a href="https://space.bilibili.com/480883651" target="_black"><span class="badge-subject">Design by</span><span class="badge-value bg-brightgreen">早起的年轻人</span></a>
        </div>
        <div class="github-badge">
            <a href="https://cloud.tencent.com/developer/user/6702670" target="_black"><span class="badge-subject">Theme</span><span class="badge-value bg-yellowgreen">云社区</span></a>
        </div>
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script><script src="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/js/totop.js?v=0.0.0' async=""></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/js/douban.js'></script>
    <script src="/js/copy-to-clipboard.js"></script>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='https://luckly.work/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://luckly.work/">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9/" title="Nginx文件内容">Nginx文件内容</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%9E%81%E7%AE%80%E6%95%99%E7%A8%8B/" title="Nginx极简教程">Nginx极简教程</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/%E5%8D%81%E5%88%86%E9%92%9F%E5%85%A5%E9%97%A8nginx/" title="十分钟入门Nginx">十分钟入门Nginx</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/go/Goland%E8%BF%9C%E7%A8%8B%E5%BC%80%E5%8F%91%E9%85%8D%E7%BD%AE/" title="Goland远程开发配置">Goland远程开发配置</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E8%AF%BB%E4%B9%A6/%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90/" title="域名解析">域名解析</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/git/%E6%8F%90%E4%BA%A4%E8%BF%87%E7%A8%8B%E7%9C%81%E7%95%A5%E6%9F%90%E4%BA%9B%E6%96%87%E4%BB%B6/" title="提交过程省略某些文件">提交过程省略某些文件</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_DropdownButton%E7%A4%BA%E4%BE%8B/" title="Flutter_DropdownButton示例">Flutter_DropdownButton示例</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_ExpansionPanelList%E5%92%8CExpansionPanelList.radio%E7%A4%BA%E4%BE%8B/" title="Flutter_ExpansionPanelList和ExpansionPanelList">Flutter_ExpansionPanelList和ExpansionPanelList</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter%E5%BE%AE%E4%BF%A1%E5%88%86%E4%BA%AB%E9%93%BE%E6%8E%A5%E8%B7%B3%E5%9B%9EApp%E6%8C%87%E5%AE%9A%E9%A1%B5%E9%9D%A2/" title="Flutter微信分享链接跳回App指定页面">Flutter微信分享链接跳回App指定页面</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F/%E5%A4%96%E5%8C%85%E5%8F%AF%E8%83%BD%E7%9A%84%E9%97%AE%E9%A2%98/" title="外包可能的问题">外包可能的问题</a>
    </li>
    
</ul>
    </section>

    

    <section class="widget">
        <h3 class="widget-title"><a href='/categories/'>分类</a></h3>
<ul class="widget-list">
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (326)</a></li>
    
    <li><a href="https://luckly.work/categories/IT/">IT (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Kotlin/">Kotlin (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Mysql/">Mysql (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (1)</a></li>
    
    <li><a href="https://luckly.work/categories/Vue/">Vue (6)</a></li>
    
    <li><a href="https://luckly.work/categories/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/android/">android (6)</a></li>
    
    <li><a href="https://luckly.work/categories/dart/">dart (96)</a></li>
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (28)</a></li>
    
    <li><a href="https://luckly.work/categories/gin/">gin (25)</a></li>
    
    <li><a href="https://luckly.work/categories/git/">git (4)</a></li>
    
    <li><a href="https://luckly.work/categories/Go/">Go (102)</a></li>
    
    <li><a href="https://luckly.work/categories/gorm/">gorm (4)</a></li>
    
    <li><a href="https://luckly.work/categories/grpc/">grpc (1)</a></li>
    
    <li><a href="https://luckly.work/categories/html/">html (3)</a></li>
    
    <li><a href="https://luckly.work/categories/ios/">ios (1)</a></li>
    
    <li><a href="https://luckly.work/categories/linux/">linux (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (6)</a></li>
    
    <li><a href="https://luckly.work/categories/python/">python (35)</a></li>
    
    <li><a href="https://luckly.work/categories/read/">读书笔记 (6)</a></li>
    
    <li><a href="https://luckly.work/categories/redis/">redis (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E5%8D%95/">书单 (8)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E8%AF%84/">书评 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师 (19)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师 (25)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E5%88%99/">原则 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%9F%BA%E7%A1%80/">基础 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%BF%83%E8%AF%AD/">心语 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%8F%92%E4%BB%B6/">插件 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%95%99%E5%AD%A6/">教学 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%B2%BE%E8%BF%9B/">精进 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%BC%96%E7%A8%8B/">编程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%99%9A%E5%B9%BB/">虚幻 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%91%E6%AD%A5/">跑步 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%BA%BF/">路线 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%A1%B9%E7%9B%AE/">项目 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%AD%85%E5%8A%9B/">魅力 (1)</a></li>
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title"><a href='/tags/'>标签</a></h3>
<div class="tagcloud">
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/IT/">IT</a>
    
    <a href="https://luckly.work/tags/Kotlin/">Kotlin</a>
    
    <a href="https://luckly.work/tags/Mysql/">Mysql</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/Vue/">Vue</a>
    
    <a href="https://luckly.work/tags/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载</a>
    
    <a href="https://luckly.work/tags/android/">android</a>
    
    <a href="https://luckly.work/tags/dart/">dart</a>
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/gin/">gin</a>
    
    <a href="https://luckly.work/tags/git/">git</a>
    
    <a href="https://luckly.work/tags/go/">go</a>
    
    <a href="https://luckly.work/tags/gorm/">gorm</a>
    
    <a href="https://luckly.work/tags/grpc/">grpc</a>
    
    <a href="https://luckly.work/tags/html/">html</a>
    
    <a href="https://luckly.work/tags/ios/">ios</a>
    
    <a href="https://luckly.work/tags/linux/">linux</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/python/">python</a>
    
    <a href="https://luckly.work/tags/redis/">redis</a>
    
    <a href="https://luckly.work/tags/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E5%8D%95/">书单</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E8%AF%84/">书评</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师</a>
    
    <a href="https://luckly.work/tags/%E5%85%A5%E9%97%A8/">入门</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E5%88%99/">原则</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯</a>
    
    <a href="https://luckly.work/tags/%E5%9F%BA%E7%A1%80/">基础</a>
    
    <a href="https://luckly.work/tags/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应</a>
    
    <a href="https://luckly.work/tags/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程</a>
    
    <a href="https://luckly.work/tags/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路</a>
    
    <a href="https://luckly.work/tags/%E5%BF%83%E8%AF%AD/">心语</a>
    
    <a href="https://luckly.work/tags/%E6%8F%92%E4%BB%B6/">插件</a>
    
    <a href="https://luckly.work/tags/%E6%95%99%E5%AD%A6/">教学</a>
    
    <a href="https://luckly.work/tags/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明</a>
    
    <a href="https://luckly.work/tags/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书</a>
    
    <a href="https://luckly.work/tags/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单</a>
    
    <a href="https://luckly.work/tags/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫</a>
    
    <a href="https://luckly.work/tags/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸</a>
    
    <a href="https://luckly.work/tags/%E7%B2%BE%E8%BF%9B/">精进</a>
    
    <a href="https://luckly.work/tags/%E7%BC%96%E7%A8%8B/">编程</a>
    
    <a href="https://luckly.work/tags/%E8%99%9A%E5%B9%BB/">虚幻</a>
    
    <a href="https://luckly.work/tags/%E8%AF%97/">诗</a>
    
    <a href="https://luckly.work/tags/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/">读书笔记</a>
    
    <a href="https://luckly.work/tags/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路</a>
    
    <a href="https://luckly.work/tags/%E8%B7%91%E6%AD%A5/">跑步</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%BA%BF/">路线</a>
    
    <a href="https://luckly.work/tags/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施</a>
    
    <a href="https://luckly.work/tags/%E9%80%9A%E8%AE%AF%E5%BD%95/">通讯录</a>
    
    <a href="https://luckly.work/tags/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学</a>
    
    <a href="https://luckly.work/tags/%E9%A1%B9%E7%9B%AE/">项目</a>
    
    <a href="https://luckly.work/tags/%E9%AD%85%E5%8A%9B/">魅力</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="http://www.topgoer.com/" title="枯藤">枯藤</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gorm.cn/zh_CN/docs/index.html" title="gorm">gorm</a>
        </li>
        
        <li>
            <a target="_blank" href="https://docs.python.org/zh-cn/3/tutorial/index.html" title="python">python</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.liwenzhou.com/" title="李文周">李文周的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://www.xbzweb.com/" title="小包子的博客">小包子的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.flysnow.org/" title="飞雪无情的博客">飞雪无情的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://sliverhorn.com/" title="sliverhorn的博客">sliverhorn的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://yuedu.baidu.com/ebook/14a722970740be1e640e9a3e" title="Android Gradle权威指南">Android Gradle权威指南</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gesdh.cn/" title="小格子">格子导航</a>
        </li>
        
        <li>
            <a target="_blank" href="https://itachi.xyz/" title="阿林">itachi&#39;s Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://darjun.github.io/" title="大俊">大俊Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://geektutu.com/post/quick-golang.html" title="极客兔兔">极客兔兔Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="http://zxfcumtcs.github.io/" title="赵雪峰">雪峰Blog</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="https://luckly.work/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>
</body>

</html>